package com.example.appxi;

import com.navdrawer.SimpleSideDrawer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class LeftSlideMenu extends ActionBarActivity implements View.OnClickListener
{
	Button btn_searchPlace;
	Button btn_event;
	Button btn_issue;
	Button btn_friChat;
	public Activity top;
	private SimpleSideDrawer mNav;
	private BackPressCloseHandler backPressCloseHandler;
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
     // 액션바를 가져왔다
     	android.app.ActionBar bar = getActionBar();
     	// 뷰를 가져옴
     	View v = getLayoutInflater().inflate(R.layout.custombar, null);
     		
     	// 액션바에 커스텀뷰를 설정
     	bar.setCustomView(v);
     		
     	// 커스텀 뷰를 써야하므로 옵션에서 설정
     	bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
     	
     	mNav = new SimpleSideDrawer(this);
        mNav.setLeftBehindContentView(R.layout.activity_behind_left_simple);
        Button btnLeftSlide = (Button) findViewById(R.id.bt_leftslide);
        
        btnLeftSlide.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mNav.toggleLeftDrawer();
            }
        });
        btn_searchPlace=(Button) findViewById(R.id.btn_searchPlace);
        btn_searchPlace.setOnClickListener(this);
        btn_event=(Button)findViewById(R.id.btn_event);
        btn_event.setOnClickListener(this);
        btn_issue=(Button)findViewById(R.id.btn_issue);
        btn_issue.setOnClickListener(this);
        btn_friChat=(Button)findViewById(R.id.btn_friChat);
        btn_friChat.setOnClickListener(this);
        
        backPressCloseHandler = new BackPressCloseHandler(this);
        
        final Button btn_me = (Button) findViewById(R.id.bt_me);
        btn_me.setOnClickListener(new OnClickListener() {
        	public void onClick(View view){
                if(view==btn_me){ //view가 alert 이면 팝업실행 즉 버튼을 누르면 팝업창이 뜨는 조건
                    Context mContext = getApplicationContext();
                    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
         
                    //R.layout.dialog는 xml 파일명이고  R.id.popup은 보여줄 레이아웃 아이디
                    View layout = inflater.inflate(R.layout.popupwindow,(ViewGroup) findViewById(R.id.popup));
                    AlertDialog.Builder aDialog = new AlertDialog.Builder(top);
                 
                    aDialog.setTitle("내 정보"); //타이틀바 제목
                    aDialog.setView(layout); //dialog.xml 파일을 뷰로 셋팅
                 
                    //그냥 닫기버튼을 위한 부분
                    aDialog.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                //팝업창 생성
                AlertDialog ad = aDialog.create();
                ad.show();//보여줌!
            }
        	}
        });
       
    }
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch(v.getId())
		{
		case R.id.btn_searchPlace :
			mNav.close();
			if(top.getClass()==MainActivity.class) break;
			intent = new Intent(top, MainActivity.class);
        	startActivity(intent);
			break;
			
		case R.id.btn_event :
			mNav.close();
			if(top.getClass()==BannerActivity.class) break;
			intent = new Intent(top, BannerActivity.class);
        	startActivity(intent);
			break;
			
		case R.id.btn_issue :
			mNav.close();
			if(top.getClass()==MainActivity.class) break;
			intent = new Intent(top, MainActivity.class);
        	startActivity(intent);
			break;

		case R.id.btn_friChat :
			mNav.close();
			if(top.getClass()==FriendActivity.class) break;
			intent = new Intent(top, FriendActivity.class);
        	startActivity(intent);
			break;
		}
	}

	public void onBackPressed() {
		//super.onBackPressed();
		backPressCloseHandler.onBackPressed(top);
	}
	
}
